
local ChatBox = {}

DataTemp = require("DataTemp")

ENV = CS.UnityEngine
GameObject = CS.UnityEngine.GameObject

local scroll = nil
local itempref = nil
local maxCount = 0
local total = 12
local padding
local contentBox
local itemsize
local scrollsize
local initpos
topIndex = 0
local bottomIndex
local currentOffsetY
local lists = {}
local datas = {}

function start()

    itempref = ENV.Resources.Load("uitem").gameObject
    scroll = GameObject.Find("Scrollview"):GetComponent("ScrollRect")
    contentBox = GameObject.Find("Content"):GetComponent("RectTransform")
    scrollsize = scroll:GetComponent("RectTransform").sizeDelta
    itemsize = itempref:GetComponent("RectTransform").sizeDelta
    -- itempref:GetComponent("RectTransform").sizeDelta = ENV.Vector3(contentBox.sizeDelta.x-1,itemsize.y,0)
    itemsize.y = itemsize.y + 2 + 2 -- 代处理
    initpos = itempref:GetComponent("RectTransform").position
    initpos.x = 0
    -- 计算最大显示的数量
    maxCount = math.ceil(scrollsize.y/itemsize.y)+1
    print(maxCount)
    -- maxCount = 4 --测试
    contentBox.sizeDelta = ENV.Vector2(contentBox.sizeDelta.x,total*itemsize.y)
        -- 初始化数据
    initData(total)
    for i = 1,maxCount do
        local itemobj = GameObject.Instantiate(itempref,contentBox):GetComponent("Item")
        itemobj:setData(datas[i])
        local newvec  = ENV.Vector2(initpos.x,initpos.y - (i-1) *itemsize.y)
        itemobj:GetComponent("RectTransform").anchoredPosition = newvec
        local k = "key"..i;
        lists[k] = itemobj
    end



    -- 初始化最小和最大的下标
    topIndex = 1
    bottomIndex = maxCount
    currentOffsetY = contentBox.anchoredPosition.y
    -- 绑定scrollRect事件

end


function update()
    local offsetY = contentBox.anchoredPosition.y
    if contentBox.anchoredPosition.y < 0 then
       takeDown(0)
       currentOffsetY = offsetY
       return
    end

    if contentBox.anchoredPosition.y >= (contentBox.sizeDelta.y - scrollsize.y) then
        takeUp(contentBox.sizeDelta.y-scrollsize.y)
        currentOffsetY = offsetY
        return
    end

    if offsetY > currentOffsetY then
        takeUp(contentBox.anchoredPosition.y)
    else
        takeDown(contentBox.anchoredPosition.y)
    end

end

function takeDown(y)
    
 while true do
     local h = ((bottomIndex)*itemsize.y)-(y+scrollsize.y);
        if h>=itemsize.y then
            if topIndex == 1 then
                return
            end
            local targetitem = getItemfromIndex(bottomIndex)
            removeItemfromIndex(bottomIndex)
            local targety = getItemfromIndex(topIndex):GetComponent("RectTransform").anchoredPosition.y + itemsize.y
            targetitem:GetComponent("RectTransform").anchoredPosition = ENV.Vector2(initpos.x,targety)
            topIndex = topIndex - 1
            targetitem:setData(datas[topIndex])
            bottomIndex = bottomIndex - 1
            insertItem(targetitem, topIndex)
        else
            break
        end
     end

end


function takeUp(y)

    while true do
      local ts = getItemfromIndex(topIndex):GetComponent("RectTransform")
      if -ts.anchoredPosition.y + itemsize.y/2 <= y then
        if bottomIndex == total  then
            return
        end
        local targetitem = getItemfromIndex(topIndex)
        removeItemfromIndex(topIndex)
        topIndex = topIndex + 1
        local targety = getItemfromIndex(bottomIndex):GetComponent("RectTransform").anchoredPosition.y - itemsize.y
        targetitem:GetComponent("RectTransform").anchoredPosition = ENV.Vector2(initpos.x,targety)
        bottomIndex = bottomIndex + 1
        -- print("item = ",item)
        targetitem:setData(datas[bottomIndex])
        insertItem(targetitem, bottomIndex)
      else
        break 
      end
    end
end

-- 根据key获取指定元素
function getItemfromIndex(ks)
    ksw = 'key'..ks
    for k,v in pairs(lists) do
        if ksw == k then
            return v
        end
    end
    return nil
end

-- 根据key删除指定的元素
function removeItemfromIndex(ks)

    local item = getItemfromIndex(ks)
    local i,max = 1,#lists
    while i <= max do
        if lists[i] == item then
            table.remove(lists, i)
            return
        end
        i = i + 1
    end
end

-- 加入item到列表中,需要改变他的key值
function insertItem(item,index)
    local key = "key"..index
    lists[key] = item
end


function getItem(a,b)
    print("Go !!")
    print(a,b)
    return a + b
end

-- 外界传递的参数进来，新增一个item
function ChatBox.pushData(data)
   total = total + 1
   local item = DataTemp.new(data.name, data.time, data.say, data.uid)
   table.insert(datas,#datas + 1, item)
   print("size",#datas)
   contentBox.sizeDelta = ENV.Vector2(contentBox.sizeDelta.x,total*itemsize.y)

   for i =1,#datas do
    print(datas[i].name)
end
end

function initData(count)
    for i =1,count do
        local item = DataTemp.new("name"..i, "time"..i, "say"..i, "uface0")
        table.insert(datas,#datas + 1, item)
    end


end

return ChatBox